#include "../Framework.h"
#include "../Image/ImageLib.h"
using namespace ImageLib;
PaintClass::PaintClass()
{
	m_hWnd = NULL;
	BackBuffer = NULL;
	g_RenderList.clear();
}
PaintClass::~PaintClass(void)
{

}
void PaintClass::AppendRenderChild(GuiInfo* lpGui)
{
	g_RenderList.push_back(lpGui);
}
void PaintClass::Clear()
{
	g_RenderList.clear();
}
void PaintClass::Clear(DWORD dwColor)
{
	Image *ptr = (Image*)BackBuffer;
	ptr->FillColor(0,0,ptr->GetWidth(),ptr->GetHeight(),dwColor);
}
HWND PaintClass::GetHWND()
{
	return m_hWnd;
}
HDC PaintClass::GetPaintDC()
{
	return m_hDC;
}
LONG PaintClass::GetPaintHeight()
{
	return m_nHeight;
}
LONG PaintClass::GetPaintWidth()
{
	return m_nWidth;
}
BOOL PaintClass::InitPaint(HWND hWnd,int nWidth,int nHeight)
{
	if (hWnd == NULL)	return FALSE;
	if (nWidth == 0 || nHeight == 0) return FALSE;
	if (NULL !=BackBuffer) return FALSE;
	m_hWnd = hWnd;
	Resize(nWidth,nHeight);
	if (BackBuffer == NULL)	return FALSE;
	if (0 == TextureWidth(BackBuffer) ||
		0 == TextureHeight(BackBuffer))
		return FALSE;
	m_hDC = GetDC(m_hWnd);
	if (!m_hDC)	return FALSE;
	m_nWidth = nWidth;
	m_nHeight = nHeight;
	return TRUE;
}
void PaintClass::Resize(int nWidth,int nHeight)
{
	if (nWidth == 0 || nHeight == 0) return ;
	Image* ptr = (Image*)BackBuffer;
	if (NULL == ptr ||
		nWidth != ptr->GetWidth() ||
		nHeight != ptr->GetHeight())
	{
		HTEXTURE hTex = TextureCreate(nWidth,nHeight);
		if (hTex == NULL)	return;
		BackBuffer = hTex;
	}
	m_nWidth = nWidth;
	m_nHeight = nHeight;
}
size_t PaintClass::GetRenderSize()
{
	return g_RenderList.size();
}
RENDLIST PaintClass::GetRenderList()
{
	return g_RenderList;
}
BOOL PaintClass::BitBltToDC(int nDestX,int nDesty,int nBackX,int nBackY,int nBackW,int nBackH)
{
	if (BackBuffer)
	{
		Image *ptr = (Image*)BackBuffer;
		ptr->BltToDC(m_hDC,nDestX,nDesty,nBackX,nBackY,nBackW,nBackH);
	}
	return FALSE;
}
BOOL PaintClass::AlphaBlt(int dx,int dy,HTEXTURE pSrc,BYTE alpha,int sx,int sy,int sw,int sh)
{
	if (BackBuffer)
	{
		Image *ptr = (Image*)BackBuffer;
		Image *ps = (Image*)pSrc;
		if (ps)
		{
			ptr->AlphaBlt(dx,dy,ps,alpha,sx,sy,sw,sh);
			return TRUE;
		}
	}
	return FALSE;
}
void PaintClass::SetDirty(RECT &rcDirty)
{
	RECT rcLast = m_DirtyRect;
	UnionRect(&m_DirtyRect,&rcLast,&rcDirty);
}
BOOL PaintClass::IsDirty()
{
	return (m_DirtyRect.right - m_DirtyRect.left) != 0;
}
RECT& PaintClass::GetDirtyRect()
{
	return m_DirtyRect;
}
void PaintClass::SetRectEmpty()
{
	::SetRectEmpty(&m_DirtyRect);
}